﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.DataVisualization;
using System.Windows.Controls.DataVisualization.Charting;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace DynamicChart
{
    public static class PaletteUtil
    {
        public static ResourceDictionaryCollection BuildPieSeriesPalette(IEnumerable<DynamicChart.UI.ServiceRef.ChartDataItem> items, Collection<ResourceDictionary> originalCollection)
        {
            ResourceDictionaryCollection dictCollection = new ResourceDictionaryCollection();

            foreach(var item in items)
            {
                string colorValue = item.Color;
                if (string.IsNullOrEmpty(colorValue))
                {
                    if (originalCollection == null || originalCollection.Count <= dictCollection.Count)
                    {
                        ResourceDictionary dict = new ResourceDictionary();
                        int r = new Random().Next(1, 244);
                        int g = new Random().Next(1, 244);
                        int b = new Random().Next(1, 244);
                        Color color = Color.FromArgb(0xff, (byte)r, (byte)g, (byte)b);

                        Style style = new Style(typeof(PieDataPoint));
                        style.Setters.Add(new Setter(PieDataPoint.BackgroundProperty, new SolidColorBrush(color)));
                        dict.Add("DataPointStyle", style);
                        dictCollection.Add(dict);
                    }
                    else
                    {
                        dictCollection.Add(originalCollection[dictCollection.Count]);
                    }
                }
                else
                {
                    ResourceDictionary dict = new ResourceDictionary();
                    int r = Convert.ToInt16(colorValue.Substring(colorValue.Length - 6, 2), 16);
                    int g = Convert.ToInt16(colorValue.Substring(colorValue.Length - 4, 2), 16);
                    int b = Convert.ToInt16(colorValue.Substring(colorValue.Length - 2, 2), 16);
                    Color color = Color.FromArgb(0xff, (byte)r, (byte)g, (byte)b);

                    Style style = new Style(typeof(PieDataPoint));
                    style.Setters.Add(new Setter(PieDataPoint.BackgroundProperty, new SolidColorBrush(color)));
                    dict.Add("DataPointStyle", style);

                    dictCollection.Add(dict);
                }
            }
            return dictCollection;
        }

        public static ResourceDictionaryCollection BuildColumnSeriesPalette(IEnumerable<DynamicChart.UI.ServiceRef.ChartDataItem> items, Collection<ResourceDictionary> originalCollection)
        {
            ResourceDictionaryCollection dictCollection = new ResourceDictionaryCollection();

            foreach (var item in items)
            {
                string colorValue = item.Color;
                if (string.IsNullOrEmpty(colorValue))
                {
                    if (originalCollection.Count <= dictCollection.Count)
                    {
                        ResourceDictionary dict = new ResourceDictionary();
                        int r = new Random().Next(1, 244);
                        int g = new Random().Next(1, 244);
                        int b = new Random().Next(1, 244);
                        Color color = Color.FromArgb(0xff, (byte)r, (byte)g, (byte)b);

                        Style style = new Style(typeof(ColumnDataPoint));
                        style.Setters.Add(new Setter(ColumnDataPoint.BackgroundProperty, new SolidColorBrush(color)));
                        dict.Add("DataPointStyle", style);
                        dictCollection.Add(dict);
                    }
                    else
                    {
                        dictCollection.Add(originalCollection[dictCollection.Count]);
                    }
                }
                else
                {
                    ResourceDictionary dict = new ResourceDictionary();
                    int r = Convert.ToInt16(colorValue.Substring(colorValue.Length - 6, 2), 16);
                    int g = Convert.ToInt16(colorValue.Substring(colorValue.Length - 4, 2), 16);
                    int b = Convert.ToInt16(colorValue.Substring(colorValue.Length - 2, 2), 16);
                    Color color = Color.FromArgb(0xff, (byte)r, (byte)g, (byte)b);

                    Style style = new Style(typeof(ColumnDataPoint));
                    style.Setters.Add(new Setter(ColumnDataPoint.BackgroundProperty, new SolidColorBrush(color)));
                    dict.Add("DataPointStyle", style);
                    dictCollection.Add(dict);
                }
            }
            return dictCollection;
        }
    }
}
